package dbmanager.connectionManager;

import javax.swing.JDialog;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Button;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;

import dbmanager.connectionManager.menu.AddConnectionItem;
import dbmanager.connectionManager.menu.AddFolderItem;
import dbmanager.struct.conTypes.MysqlCon;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import java.awt.Dimension;

public class AccountSelector extends JDialog {
	private JTree connTree;
	MysqlCon mysqlDefaultCon = new MysqlCon();
	TreeModel navTreeModel;
	private JTabbedPane tabbedPane;

	public AccountSelector() {
		mysqlDefaultCon.setName("New Connection");
		navTreeModel = new TreeModel(new DefaultMutableTreeNode(mysqlDefaultCon));

		setPreferredSize(new Dimension(450, 300));
		setSize(new Dimension(450, 300));

		JSplitPane splitPane = new JSplitPane();

		JButton btnSave = new JButton("Save");

		JButton btnOpen = new JButton("Open");

		JButton btnCancel = new JButton("Cancel");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addContainerGap().addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(splitPane, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE).addGroup(groupLayout.createSequentialGroup().addComponent(btnSave).addPreferredGap(ComponentPlacement.RELATED, 227, Short.MAX_VALUE).addComponent(btnOpen).addPreferredGap(ComponentPlacement.RELATED).addComponent(btnCancel))).addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(splitPane, GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE).addGap(4).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(btnSave).addComponent(btnOpen).addComponent(btnCancel)).addContainerGap()));

		JScrollPane scrollPane = new JScrollPane();
		splitPane.setLeftComponent(scrollPane);

		connTree = new JTree(navTreeModel);
		scrollPane.setViewportView(connTree);

		connTree.setRootVisible(false);
		connTree.setShowsRootHandles(false);

		connTree.addMouseListener(new TreePopup(connTree, splitPane.getLeftComponent()));

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		splitPane.setRightComponent(tabbedPane);
		getContentPane().setLayout(groupLayout);
	}

	public void addTab(String title, JComponent tab) {
		tabbedPane.addTab(title, tab);
	}

	public void removeAllTabs() {
		for (int i = 0; i < tabbedPane.getTabCount(); i++) {
			tabbedPane.removeTabAt(i);
		}
	}
}
